package com.rainbow.modules.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rainbow.common.util.SecurityUtil;
import com.rainbow.common.vo.PageVo;
import com.rainbow.common.vo.SearchVo;
import com.rainbow.modules.sys.entity.Department;
import com.rainbow.modules.sys.entity.Permission;
import com.rainbow.modules.sys.entity.Role;
import com.rainbow.modules.sys.entity.User;
import com.rainbow.modules.sys.mapper.UserMapper;
import com.rainbow.modules.sys.service.DepartmentService;
import com.rainbow.modules.sys.service.PermissionService;
import com.rainbow.modules.sys.service.UserRoleService;
import com.rainbow.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Auth rainbow
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private SecurityUtil securityUtil;

    @Override
    public User findByUsername(String username) {
        List<User> list = list(new QueryWrapper<User>().eq("username", username));
        if (list != null && list.size() > 0) {
            User user = list.get(0);
            // 关联部门
            if (StrUtil.isNotBlank(user.getDepartmentId())) {
                Department department = departmentService.getById(user.getDepartmentId());
                user.setDepartmentTitle(department.getTitle());
            }
            // 关联角色
            List<Role> roleList = userRoleService.findByUserId(user.getId());
            user.setRoles(roleList);
            // 关联权限菜单
            List<Permission> permissionList = permissionService.findByUserId(user.getId());
            user.setPermissions(permissionList);
            return user;
        }
        return null;
    }

    @Override
    public User findByMobile(String mobile) {
        List<User> list = list(new QueryWrapper<User>().eq("mobile", mobile));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public User findByEmail(String email) {
        List<User> list = list(new QueryWrapper<User>().eq("email", email));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public IPage<User> findByCondition(User user, SearchVo searchVo, PageVo pageVo) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(user.getUsername())) {
            queryWrapper.like("username", user.getUsername());
        }
        if (StrUtil.isNotBlank(user.getMobile())) {
            queryWrapper.like("mobile", user.getMobile());
        }
        if (StrUtil.isNotBlank(user.getEmail())) {
            queryWrapper.like("email", user.getEmail());
        }
        if (StrUtil.isNotBlank(user.getDepartmentId())) {
            queryWrapper.eq("department_id", user.getDepartmentId());
        }
        if (user.getSex() != null) {
            queryWrapper.eq("sex", user.getSex());
        }
        if (user.getType() != null) {
            queryWrapper.eq("type", user.getType());
        }

        if (user.getStatus() != null) {
            queryWrapper.eq("status", user.getStatus());
        }

        if (user.getCreateTime() != null) {
            //创建时间
            if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
                Date start = DateUtil.parse(searchVo.getStartDate());
                Date end = DateUtil.parse(searchVo.getEndDate());
                queryWrapper.between("create_time", start, DateUtil.endOfDay(end));
            }
        }

        //数据权限
        List<String> depIds = securityUtil.getDeparmentIds();
        if (depIds != null && depIds.size() > 0) {
            queryWrapper.in("department_id", depIds);
        }

        return this.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), queryWrapper);

    }

    @Override
    public List<User> findByDepartmentId(String departmentId) {
        return list(new QueryWrapper<User>().eq("department_id", departmentId));
    }
}
